package com.hymjweb.demo.hymj.finance_manage.repayment.mapper;

import com.hymjweb.demo.framework.sys.SysConstant;
import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.finance_manage.repayment.bean.CwRepaymentPojo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Mapper
@Component
public interface CwRepaymentMapper {


    class CwRepaymentMapperProvider {

        public String insert(CwRepaymentPojo cwRepaymentPojo) {
            String sql = new SQL() {{
                INSERT_INTO("cw_repayment");
                VALUES("id", "#{id}");
                VALUES("customer_id", "#{customerId}");
                VALUES("repayment_way_id", "#{repaymentWayId}");
                VALUES("amount", "#{amount}");
                VALUES("available_amount", "#{availableAmount}");
                VALUES("regist_user_id", "#{registUserId}");
                VALUES("regist_date", "#{registDate}");
                VALUES("voucher_file_id", "#{voucherFileId}");
                VALUES("audit_status_id", "#{auditStatusId}");
                VALUES("audit_user_id", "#{auditUserId}");
                VALUES("audit_date", "#{auditDate}");
                VALUES("comment", "#{comment}");

                VALUES("create_time", "#{createTime}");
                VALUES("create_user_id", "#{createUserId}");
                VALUES("update_time", "#{updateTime}");
                VALUES("update_user_id", "#{updateUserId}");
            }}.toString();
            return sql;
        }


        public String queryRepaymentList(Map param) {
            String sql = new SQL() {{
                SELECT("t.*,c.phone,c.address ");
                FROM("cw_repayment t left join b_customer c on t.customer_id = c.id ");
                WHERE("1=1 ");
            }}.toString();

            if (param != null) {

                String keyCustomerId = DataTypeUtil.toStringObject(param.get("keyCustomerId"));
                Long keyRepaymentWayId = DataTypeUtil.toLongObject(param.get("keyRepaymentWayId"));
                Long keyAuditStatusId = DataTypeUtil.toLongObject(param.get("keyAuditStatusId"));
                String keRegistUserId = DataTypeUtil.toStringObject(param.get("keRegistUserId"));

                String keyRegistStartDate = DataTypeUtil.toNotNullString(param.get("keyRegistStartDate"));
                String keyRegistEndDate = DataTypeUtil.toNotNullString(param.get("keyRegistEndDate"));

                String czyId = DataTypeUtil.toStringObject(param.get("czyId"));
                String departmentId = DataTypeUtil.toStringObject(param.get("departmentId"));

                if (keyCustomerId != null) {
                    sql += " and t.customer_id = " + DataTypeUtil.quoteStr(keyCustomerId);
                }

                if (keyRepaymentWayId != null) {
                    sql += " and t.repayment_way_id = " + keyRepaymentWayId;
                }

                if (keyAuditStatusId != null) {
                    sql += " and t.audit_status_id = " + keyAuditStatusId;
                }

                if (keRegistUserId != null) {
                    sql += " and t.regist_user_id = " + DataTypeUtil.quoteStr(keRegistUserId);
                }

                if (!"".equals(keyRegistStartDate)) {
                    if (!"".equals(keyRegistEndDate)) {
                        int d = keyRegistStartDate.compareTo(keyRegistEndDate);
                        if (d > 0) {
                            String tempDate = keyRegistStartDate;
                            keyRegistStartDate = keyRegistEndDate;
                            keyRegistEndDate = tempDate;
                        }
                        sql += " and (date_format(t.create_time,'%Y-%m-%d') between " + DataTypeUtil.quoteStr(keyRegistStartDate) + " and " + DataTypeUtil.quoteStr(keyRegistEndDate) + ")";
                    } else {
                        sql += " and date_format(t.create_time,'%Y-%m-%d') >= " + DataTypeUtil.quoteStr(keyRegistStartDate);
                    }
                } else {
                    if (!"".equals(keyRegistEndDate)) {
                        sql += " and date_format(t.create_time,'%Y-%m-%d') <= " + DataTypeUtil.quoteStr(keyRegistEndDate);
                    }
                }

            }

            return sql;
        }


        public String queryReverseList(Map param) {
            String sql = new SQL() {{
                SELECT("t.customer_id,b.name,b.phone,b.address,sum(t.available_amount) total_available_amount ");
                FROM("cw_repayment t left join b_customer b on t.customer_id = b.id ");
                WHERE(" 1=1 and t.audit_status_id = " + SysConstant.XT_DIC_AUDIT_STATUS.AUDITED.getId());
            }}.toString();

            if (param != null) {

                String keyCustomerId = DataTypeUtil.toStringObject(param.get("keyCustomerId"));

                String czyId = DataTypeUtil.toStringObject(param.get("czyId"));
                String departmentId = DataTypeUtil.toStringObject(param.get("departmentId"));

                if (keyCustomerId != null) {
                    sql += " and t.customer_id = " + DataTypeUtil.quoteStr(keyCustomerId);
                }

            }

            sql += " group by t.customer_id,b.name,b.phone,b.address";

            return sql;
        }

        public String getCwRepaymentPojoById(String id) {
            String sql = new SQL() {{
                SELECT("t.*");
                FROM("cw_repayment t ");
                WHERE("t.id = #{id} ");
            }}.toString();
            return sql;
        }


        public String queryAuditedRepaymentListByCustomerId(String customerId) {
            String sql = new SQL() {{
                SELECT("t.*");
                FROM("cw_repayment t ");
                WHERE("t.customer_id = #{customerId} and t.audit_status_id = " + SysConstant.XT_DIC_AUDIT_STATUS.AUDITED.getId());
                ORDER_BY("t.create_time asc");
            }}.toString();
            return sql;
        }

        public String update(CwRepaymentPojo pojo) {
            String sql = new SQL() {{
                UPDATE("cw_repayment t");
                SET("t.customer_id = #{customerId}");
                SET("t.repayment_way_id = #{repaymentWayId}");
                SET("t.amount = #{amount}");
                SET("t.available_amount = #{availableAmount}");
                SET("t.regist_user_id = #{registUserId}");
                SET("t.regist_date = #{registDate}");
                SET("t.voucher_file_id = #{voucherFileId}");
                SET("t.audit_status_id = #{auditStatusId}");
                SET("t.audit_user_id = #{auditUserId}");
                SET("t.audit_date = #{auditDate}");
                SET("t.comment = #{comment}");

                SET("t.update_time = #{updateTime}");
                SET("t.update_user_id = #{updateUserId}");
                WHERE("t.id = #{id}");
            }}.toString();
            return sql;
        }

    }

    @InsertProvider(type = CwRepaymentMapperProvider.class, method = "insert")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(CwRepaymentPojo cwRepaymentPojo);

    @UpdateProvider(type = CwRepaymentMapperProvider.class, method = "update")
    int update(CwRepaymentPojo pojo);

    @ResultType(Map.class)
    @SelectProvider(type = CwRepaymentMapperProvider.class, method = "queryRepaymentList")
    List<Map<String, Object>> queryRepaymentList(Map param);

    @ResultType(Map.class)
    @SelectProvider(type = CwRepaymentMapperProvider.class, method = "queryReverseList")
    List<Map<String, Object>> queryReverseList(Map param);

    @Results(id = "cwRepaymentPojoMapId", value = {
            @Result(column = "id", property = "id"),
            @Result(column = "customer_id", property = "customerId"),
            @Result(column = "repayment_way_id", property = "repaymentWayId"),
            @Result(column = "amount", property = "amount"),
            @Result(column = "available_amount", property = "availableAmount"),
            @Result(column = "regist_user_id", property = "registUserId"),
            @Result(column = "regist_date", property = "registDate"),
            @Result(column = "voucher_file_id", property = "voucherFileId"),
            @Result(column = "audit_status_id", property = "auditStatusId"),
            @Result(column = "audit_user_id", property = "auditUserId"),
            @Result(column = "audit_date", property = "auditDate"),
            @Result(column = "comment", property = "comment"),

            @Result(column = "create_time", property = "createTime"),
            @Result(column = "create_user_id", property = "createUserId"),
            @Result(column = "update_time", property = "updateTime"),
            @Result(column = "update_user_id", property = "updateUserId")
    })
    @SelectProvider(type = CwRepaymentMapperProvider.class, method = "getCwRepaymentPojoById")
    CwRepaymentPojo getCwRepaymentPojoById(String id);


    @ResultMap("cwRepaymentPojoMapId")
    @SelectProvider(type = CwRepaymentMapperProvider.class, method = "queryAuditedRepaymentListByCustomerId")
    List<CwRepaymentPojo> queryAuditedRepaymentListByCustomerId(String customerId);


}
